import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * menuEmprestimo.java
 *
 * Created on 07/09/2011, 17:12:32
 */
/**
 *
 * @author Anthony
 */
public class menuEmprestimo extends javax.swing.JFrame {

    /** Creates new form menuEmprestimo */
    public menuEmprestimo() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        campoRA = new javax.swing.JTextField();
        campoNome = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(102, 102, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        campoRA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoRAActionPerformed(evt);
            }
        });
        jPanel1.add(campoRA, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 30, 70, -1));
        jPanel1.add(campoNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 70, 70, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("RA");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Nome do livro");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setText("Comfirmar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, -1, -1));

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 130, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 1, 220, 160));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
          try{ 
            Connection conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sisteca", "postgres", "postgres");
            String ra = campoRA.getText();
            String nome = campoNome.getText();
            String sql = "select ra,qtd from cliente where ra = ? ";
            
            PreparedStatement query = conexao.prepareStatement(sql);
            query.setString(1, ra);
            ResultSet rs = query.executeQuery();
            
            if(rs.next()){
                int aux = rs.getInt("qtd");
                    if(aux < 3){
                       sql = "UPDATE cliente SET qtd=qtd+1 WHERE ra = ?";
                       query = conexao.prepareStatement(sql);
                       query.setString(1, ra);
                       query.execute();
                       
                       sql = "INSERT INTO emprestimo VALUES(?,?,now(),date(now())+7,null)";
                       query = conexao.prepareStatement(sql);
                       query.setString(1, ra);
                       query.setString(2, nome);
                       query.execute();
                       
                       this.dispose();
                       JOptionPane.showMessageDialog(null, "Emprestimo efetuado!"); 
                    }
              }
            else{
                if(campoRA.getText().equals("") || campoNome.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "Campo(s) em branco!");
                }
                else{
                    JOptionPane.showMessageDialog(null, "RA ou Livro Invalido!");
                }
            }
        
        query.close();
        rs.close();
           
        } catch(Exception e){
            System.out.println("Deu erro...");

        }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void campoRAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoRAActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoRAActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new menuEmprestimo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoRA;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
